<?php

class MySQLDB
{
    private $dbConfig = array(
        "host" => "127.0.0.1",
        "port" => "3310",
        "user" => "root",
        "pwd" => "root",
        "dbName" => "volunteerdb",
        "charset" => "utf8"
    );
    private $link = null;
    //私有静态实例，防止类外部访问
    private static $instance = null;

    //私有化构造方法
    private function __construct($params = array())
    {
        //初始化属性
        $this->initAttribute($params);
        //连接数据库
        $this->connectDB();
        //设置字符集
        $this->setCharset();
    }

    //私有化克隆方法,防止克隆对象
    private function __clone()
    {
    }

    //通过静态方法获得实例
    public static function getInstance($params = array())
    {
        if (empty(self::$instance)) {
            self::$instance = new self($params);
        }
        return self::$instance;
    }

    //对成员属性进行初始化
    private function initAttribute($params)
    {
        //array_merge函数合并两个数组
        $this->dbConfig = array_merge($this->dbConfig, $params);
    }

    //连接服务器方法
    protected function connectDB()
    {
        $host = $this->dbConfig["host"];
        $port = $this->dbConfig["port"];
        $user = $this->dbConfig["user"];
        $pwd = $this->dbConfig["pwd"];
        $dbName = $this->dbConfig["dbName"];
        if ($link = mysqli_connect($host, $user, $pwd, $dbName, $port)) {
            $this->link = $link;
        } else {
            die("数据库连接失败！" . mysqli_connect_error());
        }
    }

    //设置字符集
    protected function setCharset()
    {
        $charSet = $this->dbConfig["charset"];
        mysqli_set_charset($this->link, $charSet);
        // echo "设置字符集成功!";
    }

    //执行SQL语句的方法
    public function query($sql, $type, $data)
    {
        //预处理SQL语句
        $stmt = mysqli_prepare($this->link, $sql);
        if (!$stmt) {
            die("SQL语句错误：" . mysqli_error($this->link));
        }
        if (!empty($data)) {
            //动态绑定参数
            $params = [$stmt, $type];
            foreach ($data as &$v) {
                $params[] =& $v;
            }
            call_user_func_array("mysqli_stmt_bind_param", $params);
            //echo "进行了参数绑定" . "<br>";
        }
        //执行sql语句
        $sign = mysqli_stmt_execute($stmt);
        if (!$sign) {
            exit($sql . "语句执行失败!");
        }
        //获取受影响的行数
        $row = mysqli_stmt_affected_rows($stmt);
        if ($row >= 0) {
            return mysqli_stmt_affected_rows($stmt);
        } else
            return $stmt;
    }

    //处理查询结果，返回一条数据
    public function fetchRow($sql, $type = "", $data = "")
    {
        $stmt = $this->query($sql, $type, $data);
        $result = mysqli_stmt_get_result($stmt);
        return mysqli_fetch_assoc($result);
    }

    //处理查询结果，返回多条数据
    public function fetchAll($sql, $type = "", $data = "")
    {
        $stmt = $this->query($sql, $type, $data);
        $result = mysqli_stmt_get_result($stmt);
        return mysqli_fetch_all($result, MYSQLI_ASSOC);
    }

    //关闭数据库连接
    public function closeDB()
    {
        mysqli_close($this->link);
        echo "数据库连接已关闭！";
    }
}




